//
//  Toast.m
//  Weike
//
//  Created by leirz on 14-3-12.
//  Copyright (c) 2014年 winupon. All rights reserved.
//

#import "Toast.h"
#import "AppUtils.h"

#define LOADING_VIEW_TAG 100000

UIAlertView *loadingView;
static NSTimer *outTimeTimer;
MBProgressHUD *loadingHUDView;
UIView *loadingMaskView;
UIView *loadingContentView;

@implementation Toast

+ (void)makeText:(UIView *)view text:(NSString *)text
{
    [self makeText:view text:text duration:ToastDurationShort position:ToastPositionMiddle];
}

+ (void) makeText:(UIView *)view text:(NSString *)text position:(ToastPosition)position
{
    [self makeText:view text:text duration:ToastDurationShort position:position];
}

+ (void)makeText:(UIView *)view text:(NSString *)text duration:(ToastDuration)duration
{
    [self makeText:view text:text duration:duration position:ToastPositionMiddle];
}

+ (void)makeText:(UIView *)view text:(NSString *)text duration:(ToastDuration)duration position:(ToastPosition)position
{
    if ([AppUtils isEmpty:text]) {
        return;
    }
    [MBProgressHUD hideAllHUDsForView:view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.mode = MBProgressHUDModeText;
    hud.yOffset = position == ToastPositionMiddle ? -50 : 150;
    hud.detailsLabelText = text;
    hud.removeFromSuperViewOnHide = YES;
    hud.userInteractionEnabled = NO;
    NSTimeInterval delay = duration == ToastDurationLong ? TOAST_DURATION_LONG : TOAST_DURATION_SHORT;
    [hud hide:YES afterDelay:delay];
}

+ (void)makeText:(NSString *)text
{
    [self makeText:text duration:ToastDurationShort];
}

+ (void)makeText:(NSString *)text position:(ToastPosition)position
{
    [self makeText:[UIApplication sharedApplication].keyWindow text:text duration:ToastDurationShort position:position];
}

+ (void)makeText:(NSString *)text duration:(ToastDuration)duration
{
    [self makeText:[UIApplication sharedApplication].keyWindow text:text duration:duration position:ToastPositionMiddle];
}

+ (void)loading:(UIView *)view
{
    [self loading:view text:@"请稍后"];
}

+ (void)loading
{
    [self loading:[UIApplication sharedApplication].keyWindow];
}

+ (void)loading:(UIView *)view text:(NSString *)text
{
    if (loadingHUDView == nil) {
        loadingHUDView = [[MBProgressHUD alloc] initWithView:view];
        loadingHUDView.labelFont = [UIFont boldSystemFontOfSize:15];
        loadingHUDView.labelColor = [UIColor colorWithString:COLOR_E2E2E2];
        loadingHUDView.minSize = CGSizeMake(80, 80);
        [view addSubview:loadingHUDView];
    }
    loadingHUDView.labelText = text;
    [loadingHUDView show:YES];
    [AppUtils invalidateTimer:outTimeTimer];
    outTimeTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(stopOutTimeTimer) userInfo:nil repeats:NO];
}

+ (UIAlertView *)loading:(NSString *)text delegate:(id)delegate buttonTitle:(NSString *)buttonTitle
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:text delegate:delegate cancelButtonTitle:buttonTitle otherButtonTitles:nil, nil];
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [activityView startAnimating];
    if (!IS_IOS6) {
        [alert setValue:activityView forKey:@"accessoryView"];
    } else {
        activityView.frame = CGRectMake(5, 15, 20, 20);
        [alert addSubview:activityView];
    }
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [alert show];
    return alert;
}

+ (UIProgressView *)showLoadingFile:(NSString *)text target:(id)target canceld:(SEL)canceld
{
    if (loadingMaskView != nil) {
        [loadingMaskView removeFromSuperview];
        loadingMaskView = nil;
        [loadingContentView removeFromSuperview];
        loadingContentView = nil;
    }
    loadingMaskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    loadingMaskView.backgroundColor = [UIColor blackColor];
    loadingMaskView.alpha = 0.4;
    UIView *mainView = [UIApplication sharedApplication].keyWindow;
    [mainView addSubview:loadingMaskView];
    
    CGFloat y = (SCREEN_HEIGHT - 173) / 2;
    CGFloat contentWidth = SCREEN_WIDTH - 20;
    loadingContentView = [[UIView alloc] initWithFrame:CGRectMake(10, y, contentWidth, 173)];
    loadingContentView.backgroundColor = [UIColor whiteColor];
    loadingContentView.layer.cornerRadius = 10;
    loadingContentView.layer.masksToBounds = YES;
    [mainView addSubview:loadingContentView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 26, contentWidth - 20, 30)];
    titleLabel.text = text;
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = [UIColor colorWithString:COLOR_525252];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [loadingContentView addSubview:titleLabel];
    
    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 73, contentWidth - 40, 4)];
    progressView.progressTintColor = [UIColor colorWithString:COLOR_40A608];
    progressView.layer.cornerRadius = 2;
    progressView.layer.masksToBounds = YES;
    progressView.trackTintColor = [UIColor colorWithString:COLOR_E1E1E1];
    [loadingContentView addSubview:progressView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 113.5, SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor = [UIColor colorWithString:COLOR_C4C4C4];
    [loadingContentView addSubview:lineView];
    
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 114, contentWidth, 59)];
    cancelButton.backgroundColor = [UIColor colorWithString:COLOR_F1F1F1];
    [cancelButton setBackgroundImage:[UIColor imageWithString:COLOR_D1D1D1 size:CGSizeMake(contentWidth, 59)] forState:UIControlStateHighlighted];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithString:COLOR_525252] forState:UIControlStateNormal];
    [cancelButton titleLabel].font = [UIFont systemFontOfSize:16];
    [loadingContentView addSubview:cancelButton];
    [cancelButton addTarget:target action:canceld forControlEvents:UIControlEventTouchUpInside];
    return progressView;
}

+ (void)hideLoadingFile
{
    if (loadingMaskView != nil) {
        [loadingMaskView removeFromSuperview];
        loadingMaskView = nil;
        [loadingContentView removeFromSuperview];
        loadingContentView = nil;
    }
}

+ (void)stopOutTimeTimer
{
    DSLog(@"stopOutTimeTimer");
    [self loaded];
}

+ (void)loaded
{
    if (loadingHUDView) {
        [loadingHUDView hide:YES];
        [loadingHUDView removeFromSuperview];
        loadingHUDView = nil;
    }
    [AppUtils invalidateTimer:outTimeTimer];
}

+ (MBProgressHUD *)showLoading
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *loadingView = [[MBProgressHUD alloc] initWithView:view];
    loadingView.labelFont = [UIFont boldSystemFontOfSize:15];
    loadingView.labelColor = [UIColor colorWithString:COLOR_E2E2E2];
    loadingView.minSize = CGSizeMake(80, 80);
    [view addSubview:loadingView];
    return loadingView;
}

+ (void)loaded:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}

@end
